package cn.sample.moreitems.item;

import com.google.common.collect.Multimap;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.ForgeMod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class XuanyuanSwordItem extends SwordItem {

    // 随便写一个唯一 UUID，用来后面删修饰符
    private static final UUID REACH_UUID = UUID.fromString("e8212358-aaaa-4e94-b7b9-1c8022ff0c01");
    private static final UUID ARMOR_MODIFIER_ID = UUID.fromString("a5a96bbc-5c19-4c85-bb6d-6c32e5e7b5a8");

    private Map<LivingEntity, Integer> attr = new HashMap<>();


    // 额外增加的格数，这里 +2.5，即总 reach = 3 + 2.5 = 5.5 格
    private final double bonusReach = 2.5;

    public XuanyuanSwordItem() {
        super(Tiers.DIAMOND, 8, 1.6f, new Item.Properties().stacksTo(1).durability(3));
    }





    @Override
    public void appendHoverText(ItemStack p_41421_, @Nullable Level p_41422_, List<Component> p_41423_, TooltipFlag p_41424_) {
        p_41423_.add(Component.translatable("xuanyuanjian"));
        super.appendHoverText(p_41421_, p_41422_, p_41423_, p_41424_);
    }

    @Override
    public @NotNull Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(EquipmentSlot slot) {
        var map = super.getDefaultAttributeModifiers(slot);
        if (slot == EquipmentSlot.MAINHAND || slot == EquipmentSlot.OFFHAND) {

            Multimap<Attribute, AttributeModifier> newMap =
                    com.google.common.collect.ArrayListMultimap.create(map);

            // 1.20.1 实体 reach 属性
            newMap.put(ForgeMod.ENTITY_REACH.get(),
                    new AttributeModifier(REACH_UUID, "Reach sword bonus",
                            bonusReach, AttributeModifier.Operation.ADDITION));
            return newMap;
        }
        return map;
    }


    /**
     * 给玩家加 4 点护甲（2 颗甲）
     */
    public static void applyArmor(Player player) {
        AttributeInstance armor = player.getAttribute(Attributes.ARMOR);
        if (armor != null && armor.getModifier(ARMOR_MODIFIER_ID) == null) {
            armor.addPermanentModifier(new AttributeModifier(
                    ARMOR_MODIFIER_ID,
                    "Armor Sword",
                    4.0D,                      // 4 点护甲
                    AttributeModifier.Operation.ADDITION));
        }

        AttributeInstance health = player.getAttribute(Attributes.MAX_HEALTH);
        if (health != null && health.getModifier(ARMOR_MODIFIER_ID) == null) {
            health.addPermanentModifier(new AttributeModifier(
                    ARMOR_MODIFIER_ID,
                    "Armor Sword",
                    2D,
                    AttributeModifier.Operation.MULTIPLY_TOTAL));
        }
        player.setAbsorptionAmount(player.getAbsorptionAmount() + 16);
    }

    /**
     * 移除护甲
     */
    public static void removeArmor(Player player) {
        AttributeInstance armor = player.getAttribute(Attributes.ARMOR);
        if (armor != null) {
            armor.removeModifier(ARMOR_MODIFIER_ID);
        }

        player.setAbsorptionAmount(player.getAbsorptionAmount() - 16);
    }


    public Map<LivingEntity, Integer> getAttr() {
        return attr;
    }

    public void setAttr(Map<LivingEntity, Integer> attr) {
        this.attr = attr;
    }
}
